package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.infox.R;

/**
 * 订阅列表适配器
 * 
 * @author lkx Created at 2015-10-14 上午11:16:10
 * @version 1.0
 */
public class MySubscriptionListAdapter extends BaseAdapter {
	private Context context;
	private JSONArray array;
	private LayoutInflater inflater;

	public MySubscriptionListAdapter() {
	}

	public MySubscriptionListAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}

	public MySubscriptionListAdapter(Context context, JSONArray array) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.array = array;
	}

	@Override
	public int getCount() {
		return array == null ? 0 : array.length();
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.infox_fragment_subscription_item, null);
			holder.subscriptionImg = (ImageView) convertView.findViewById(R.id.subscriptionImg);
			holder.subscriptionTitle = (TextView) convertView.findViewById(R.id.subscriptionTitle);
			holder.subscriptionIntroduction = (TextView) convertView.findViewById(R.id.subscriptionIntroduction);
			holder.subscriptionMessageCount = (TextView) convertView.findViewById(R.id.messageCount);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// DisplayImageOptions defaultOptions = new
		// DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.infox_img_loadrong)
		// .showImageOnFail(R.drawable.infox_img_loadrong).showImageOnLoading(R.drawable.infox_img_loadrong).cacheInMemory(true)
		// .cacheOnDisc(true).build();
		try {

			holder.subscriptionMessageCount.setVisibility(View.INVISIBLE);

			holder.subscriptionTitle.setText(array.getJSONObject(position).getString("title"));
			holder.subscriptionIntroduction.setText(array.getJSONObject(position).getString("memo"));
			// ImageLoader.getInstance()
			// .displayImage(array.getJSONObject(position).getString("image"),
			// holder.subscriptionImg, defaultOptions);
			AppKit.SetNormalImg(array.getJSONObject(position).getString("image"), holder.subscriptionImg);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return convertView;
	}

	class ViewHolder {
		ImageView subscriptionImg;
		TextView subscriptionTitle;
		TextView subscriptionIntroduction;
		TextView subscriptionMessageCount;

	}

	/**
	 * 替换当前数据源数据
	 * 
	 * @param moreArry
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:23
	 */
	public void setData(JSONArray jsonArray) {
		array = jsonArray;
		notifyDataSetChanged();
	}

	/**
	 * 向已有数据源中追加数据
	 * 
	 * @param jsonArray
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:00
	 */
	public void appendData(JSONArray jsonArray) {
		try {
			array = AppKit.joinJSONArray(array, jsonArray);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		notifyDataSetChanged();
	}
}
